package com.qjdchina.sku.biz.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qjdchina.commons.enums.IsDeleted;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.ProductPictureManager;
import com.qjdchina.sku.converter.ProductPictureConverter;
import com.qjdchina.sku.dao.mapper.ProductPictureMapper;
import com.qjdchina.sku.enums.PicturePathType;
import com.qjdchina.sku.model.ProductPicture;
import com.qjdchina.sku.model.vo.ProductPictureVO;
import com.qjdchina.sku.service.enums.SkuResultCode;

/**
 * 图片
 * 
 * @author shenpingfeng 2016年12月1日
 */
@Service
public class ProductPictureManagerImpl implements ProductPictureManager {

	public static final Logger LOGGER = LoggerFactory.getLogger(ProductPictureManagerImpl.class);

	@Autowired
	private ProductPictureMapper productPictureMapper;


	/**
	 * 根据productId获取图片 
	 * shenpingfeng
	 * 2016年12月1日
	 * @param productId
	 * @return
	 */
	@Override
	public Result<List<ProductPictureVO>> findProductPictureByProductId(Integer productId) {
		if (productId == null) {
			return new Result<List<ProductPictureVO>>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		List<ProductPicture> productPictures = productPictureMapper.selectByProductId(productId);

		List<ProductPictureVO> productPictureVOs = ProductPictureConverter.toProductPictureVOs(productPictures);
		return new Result<List<ProductPictureVO>>(productPictureVOs);
	}
	
	/**
	 * 新增
	 * shenpingfeng
	 * 2016年12月5日
	 * @param productPicture
	 * @return
	 */
	@Override
	public Result<String> createProductPicture(ProductPicture productPicture) {
		if (productPicture == null) {
			return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		productPicture.setPicturePathType(PicturePathType.RELATIVE_PATH.getValue());
		productPicture.setIsDeleted(IsDeleted.NO.getValue());
		productPicture.setGmtCreated(new Date());
		productPicture.setGmtModified(new Date());
		int row = productPictureMapper.insertSelective(productPicture);

		if(row == 0){
			return new Result<String>(SkuResultCode.ADD_FAIL);
		}
		return new Result<String>(productPicture.getPicturePath());
	}
	/**
	 * 删除 
	 * shenpingfeng
	 * 2016年12月5日
	 * @param id
	 * @return
	 */
	@Override
	public Result<String> deleteProductPicture(Integer id) {
		if(id == null){
			return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		int row = productPictureMapper.deleteByPrimaryKey(id);
		if(row == 0){
			return new Result<String>(SkuResultCode.DELETE_FAIL);
		}
		return new Result<String>(id.toString());
	}
	/**
	 * 更新
	 * shenpingfeng
	 * 2017年2月15日
	 * @param productPictureVO
	 * @return
	 */
	@Override
	public Result<String> updateProductPicture(ProductPictureVO productPictureVO) {
		if(productPictureVO == null || productPictureVO.getId() == null){
			return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		ProductPicture productPicture = ProductPictureConverter.toProductPicture(productPictureVO);
		int row = productPictureMapper.updateByPrimaryKeySelective(productPicture);
		if(row == 0){
			return new Result<String>(SkuResultCode.UPDATE_FAIL);
		}
		return new Result<String>(productPictureVO.getPicturePath());
	}
	
	
	

}
